package com.demoandroid.demo.widget;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import com.demoandroid.demo.R;

public class Widget extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_widget);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void onClick(View view) {
        Intent intent =  new Intent();

        switch (view.getId()){
            case R.id.wid_date_btn:
                intent.setClass(this, DateWidget.class);
                break;
            case R.id.wid_time_btn:
                intent.setClass(this, TimeWidget.class);
                break;
            case R.id.wid_datetime_btn:
                // 1.获取布局
                View view1 = View.inflate(this, R.layout.activity_time_widget, null);
                // 2.获取布局中的控件
                final TimePicker datePicker = (TimePicker) view1.findViewById(R.id.wid_time);
                // 监听时间
                datePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                    @Override
                    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                        Toast.makeText(Widget.this,String.valueOf(hourOfDay) +":"+ String.valueOf(minute),Toast.LENGTH_SHORT).show();
                    }
                });
//                String timeStr = String.valueOf(datePicker.getHour()) + String.valueOf(datePicker.getMinute()) + String.valueOf(datePicker.getTag());
                new AlertDialog.Builder(this)
                        .setTitle("弹出时间选择器")
                        // 弹出框设成获取的布局
                        .setView(view1)
                        .setNeutralButton("ok",new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();
//                intent.setClass(this, DateTimeWidget.class);
                break;
            default:
                break;
        }
        if (intent.getComponent()!=null)
        startActivity(intent);
    }
}